package tech.cn.note.html.tool;

import java.util.List;

import cn.hutool.core.convert.NumberChineseFormatter;
import freemarker.template.SimpleNumber;
import freemarker.template.SimpleScalar;
import freemarker.template.TemplateModelException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.stereotype.Component;

/**
 * 将金额转换为中文
 *
 * @author liuyong
 */
@Component
public class DocumentUpMoneyConvert implements DocumentConvert {

    private Logger log = LoggerFactory.getLogger(DocumentUpMoneyConvert.class);

    //转换器名称
    private static String CONVERT_NAME = "chineseMoney";


    @Override
    public String getConvertName() {
        return CONVERT_NAME;
    }

    @Override
    public Object exec(List params) throws TemplateModelException {
        if (params == null || params.size() < 1) {
            throw new TemplateModelException("模板自定义金额转换,参数为空");
        }
        Object objParam = params.get(0);
        if (objParam == null) {
            return "";
        }
        if (objParam instanceof SimpleNumber) {
            return NumberChineseFormatter.format(Double.parseDouble(objParam.toString()), true, true);
        } else if (objParam instanceof SimpleScalar) {
            return NumberChineseFormatter.format(Double.parseDouble(objParam.toString()), true, true);
        } else {
            throw new TemplateModelException("模板自定义金额转换无法识别模板中的数据类型");
        }
    }
}
